package com.yanqu.road.server.manager.activity.unionchest;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionChest.CrossUnionChestNoticeData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.pb.CrossActivityPb;
import com.yanqu.road.pb.activity.UnionChestProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionChestMgr extends TempCrossMgr {
    private static Map<Integer, UnionChestActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        List<Integer> removeList = new ArrayList<>();
        long nowTime = DateHelper.getCurrentSecond();
        for (Integer acvitityId : activityMap.keySet()) {
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(acvitityId);
            if (null == activityInfo) {
                removeList.add(acvitityId);
                continue;
            }
            if (nowTime >= activityInfo.getEndShowTime() + DateHelper.MINUTE_SECONDS * 5) {
                removeList.add(acvitityId);
            }
        }
        if (!removeList.isEmpty()){
            for (Integer activityId : removeList) {
                activityMap.remove(activityId);
            }
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        reload();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        CrossUnionChestConfigMgr.reloadConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.UnionChest.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        // 重新加载数据
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (!activityMap.containsKey(activityInfo.getActivityId())) {
                getActivity(activityInfo.getActivityId());
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (UnionChestActivity activity : activityMap.values()) {
            activity.save();
        }
        return true;
    }

    public static UnionChestActivity getActivity(int activityId) {
        UnionChestActivity activity = activityMap.get(activityId);
        if (null == activity) {
            synchronized (activityMap) {
                activity = activityMap.get(activityId);
                if (null == activity) {
                    ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if (null == activityInfo) {
                        return null;
                    }
                    CrossUnionChestConfigMgr.reloadConfig(activityInfo);
                    activity = new UnionChestActivity(activityId);
                    activityMap.put(activityId, activity);
                }
            }
        }

        return activity;
    }

    /**
     * 商会宝箱列表
     */
    public static UnionChestProto.UnionChestUserSyncMsg.Builder getUserSyncMsg(int activityId, long userId, String unionId) {
        UnionChestProto.UnionChestUserSyncMsg.Builder builder = UnionChestProto.UnionChestUserSyncMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setDailyNum(0);
        UnionChestActivity activity = getActivity(activityId);
        if (null == activity) {
            return builder;
        }
        return activity.getUserSyncMsg(userId, unionId);
    }

    /**
     * 分享宝箱
     */
    public static UnionChestProto.CrossUnionChestShareBoxRespMsg.Builder shareBox(int activityId, long userId, String unionId, int boxType) {
        UnionChestProto.CrossUnionChestShareBoxRespMsg.Builder builder = UnionChestProto.CrossUnionChestShareBoxRespMsg.newBuilder();
        builder.setRet(0);
        builder.setActivityId(activityId);
        UnionChestActivity activity = getActivity(activityId);
        if (null == activity) {
            return builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }
        return activity.shareBox(userId, unionId, boxType).setUserId(userId);
    }

    /**
     * 开宝箱
     */
    public static UnionChestProto.CrossUnionChestOpenBoxRespMsg.Builder openBox(int activityId, long userId, String userName, String unionId, long boxId, int boxIndex) {
        UnionChestProto.CrossUnionChestOpenBoxRespMsg.Builder builder = UnionChestProto.CrossUnionChestOpenBoxRespMsg.newBuilder();
        builder.setUnionId(unionId);
        UnionChestProto.UnionChestBoxOpenBoxRespMsg.Builder respMsg = UnionChestProto.UnionChestBoxOpenBoxRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setActivityId(activityId);
        builder.setRespMsg(respMsg);
        UnionChestActivity activity = getActivity(activityId);
        if (null == activity) {
            return builder.setRespMsg(respMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND));
        }
        builder.setBoxIndex(boxIndex);
        builder.setUserId(userId);
        respMsg = activity.openBox(userId, userName, unionId, boxId, boxIndex);
        builder.setRespMsg(respMsg);

        return builder;
    }

    /**
     * 公告列表
     */
    public static UnionChestProto.UnionChestNoticeListRespMsg.Builder getNoticeListMsg(int activityId, String unionId) {
        UnionChestProto.UnionChestNoticeListRespMsg.Builder builder = UnionChestProto.UnionChestNoticeListRespMsg.newBuilder();
        builder.setRet(0);
        builder.setActivityId(activityId);
        UnionChestActivity activity = getActivity(activityId);
        if (null == activity) {
            return builder;
        }
        return activity.getNoticeListMsg(unionId);
    }

    public static void addNoticeMsg(int activityId, UnionChestProto.CrossUnionChestNoticeAddMsg msg){
        UnionChestActivity activity = getActivity(activityId);
        if (null == activity) {
            return;
        }

        activity.addNoticeMsg(msg);
    }
}
